﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using SupportLibrary;

namespace TestFramework
{
    public class DBLinq
    {
        private  DataContext datacontext = null;
        private Decimal currentBalance = -1;
        private static DBLinq linq = null;

        public DBLinq()
        {

            
            //var accounts  = from a in datacontext.GetTable<Accounts>() 
            //                where a.Status.Equals(1)
            //                select a;
                
            //foreach( Accounts acc in accounts)
            //{
            //    if(acc.Balance!=null)
            //    Console.WriteLine(acc.Mobile + ":" + acc.Balance);
            //}
        }
        public DBLinq(string connectionString)
        {

            //datacontext = new DataContext("Data Source=172.17.18.13;Initial Catalog=YepexMexicoMXME;uid=MXWallet;pwd=MXWallet123;Connection Timeout=1;");
            datacontext = new DataContext(connectionString);
        }
       
        public static DBLinq Instance()
        {
            if (linq == null) {
                string connectionString = "Data Source=172.17.18.13;Initial Catalog=YepexMexicoMXME;uid=MXWallet;pwd=MXWallet123;Connection Timeout=1;";
                linq = new DBLinq(connectionString);
            }
            return linq;
        }

        public void VerifyBalance(string mobile, Decimal ExpectedBalance)
        {
             bool verifySuccess = false;
            Decimal currentBalance = -1;
            try {
                var accounts = from a in datacontext.GetTable<Accounts>()
                               where a.Status.Equals(1) & a.Mobile.Equals(mobile)
                               select a;

                foreach (Accounts acc in accounts)
                {
                    currentBalance = acc.Balance;
                    if (acc.Balance == ExpectedBalance)

                    {
                        verifySuccess = true;
                        break;
                    }
                }

                if (!verifySuccess) {
                    throw new AutoException("CurrentBalance:" + currentBalance +Res.Err.expectedBalance_false);
                }


            }
            catch(Exception ex) {
                if(ex.GetType()!= typeof(AutoException))
                throw new AutoException(Res.Err.db_connection_failed);
            }
        }

        public void UpdateBalance(string mobile, Decimal balance)
        {
            try
            {
                var accounts  = from a in datacontext.GetTable<Accounts>() 
                            where a.Status.Equals(1) & a.Mobile.Equals(mobile)
                            select a;

                foreach (Accounts acc in accounts)
                {
                    if (acc.Balance != null)
                        acc.Balance = balance;
                }

                datacontext.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new AutoException(Res.Err.db_connection_failed);
            }
        }
    }




    [Table]
    public class Accounts
    {
        [Column(Name="AccountID",IsPrimaryKey=true)]
        public int AccountID{get;set;}
        
        [Column]
        public string Mobile { get; set; }

        [Column]
        public Decimal Balance { get; set; }

        [Column]
        public int Status { get; set; }
    }

    

}
